<?php
namespace app\access\controller;

use tp5_lib\common\controller\BaseController;
use tp5_lib\access\logic\AccessModule;
use tp5_lib\access\logic\PrivilegeLogic;
use tp5_lib\access\logic\RoleLogic;

class AdminController extends BaseController
{
    /**
     * 角色列表
     */
    public function index()
    {
        $logic=RoleLogic::instance();
        $this->assign("list",$logic->getAllRoles());
        return $this->fetch();
    }
    /**
     * 修改角色权限
     */
    public function updatePrivilege()
    {
        $data=input();
        $code=$data[0];
        $logic=RoleLogic::instance();
        $privileges=$logic->db()->where("code",$code)->find();
        $privilege=$logic->privilege();
        $privileges=$privilege->parse($privileges["privilege"]);
        $this->assign("code",$code);
        $this->assign("list",$privilege->toMenu($privileges));
        return $this->fetch("privilege");
    }
    public function doUpdatePrivilege()
    {
        $data=input();
        $logic=RoleLogic::instance();
        $privilege=$logic->privilege();
        $privileges=$privilege->fromMenu(input());
        $logic->db()->where("code",$data[0])->setField("privilege",$privileges);
        $logic->refresh();
        $this->redirect("index");
    }
}
